package com.kx.center.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kx.center.auth.Authentication;
import com.kx.center.entity.Account;
import com.kx.center.entity.MsgNotice;
import com.kx.center.entity.MsgSend;
import com.kx.center.entity.StoreAppAccount;
import com.kx.center.enumerate.MessageCode;
import com.kx.center.exception.EmptyTokenException;
import com.kx.center.exception.IllegalTokenException;
import com.kx.center.service.MsgNoticeService;
import com.kx.center.util.JSONParser;
import com.kx.center.util.Response;

/**
 * @description 消息控制器 
 * @version 1.0
 * @author Scott 
 * @date 2016年1月6日 上午11:21:15
 */
@Controller
@RequestMapping("/msg")
public class MsgController extends CommonController{

	@Autowired
	MsgNoticeService noticeService;
	
	@RequestMapping(value = "list/{pageNow}/{size}" ,method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String noticeList (HttpServletRequest request ,@PathVariable("pageNow") String pageNowStr ,@PathVariable("size") String size){
		if(!NumberUtils.isDigits(pageNowStr) || !NumberUtils.isDigits(pageNowStr)){
			//参数有误
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		Account account = this.getCommonUserAccount(request);
		if(account == null){
			//登录状态失效，返回重新登录 
			return Response.error(MessageCode.ERROR_NO_LOGIN);
		}
		List<MsgNotice> noticeList = noticeService.findNoticeListByPage
				(account.getCompanyId(),NumberUtils.toInt(pageNowStr), NumberUtils.toInt(size));
		int pageCount = noticeService.getPageCount(account.getCompanyId());
		HashMap<String, Object> noticeMap = new HashMap<>();
		noticeMap.put("datas", noticeList);
		noticeMap.put("pageCount", pageCount);
		noticeMap.put("pageNow", NumberUtils.toInt(pageNowStr));
		noticeMap.put("pageSize", NumberUtils.toInt(size));
		
		return Response.success(noticeMap);
	} 
	
	/*
	 * 根据消息id查询通知详情
	 */
	@RequestMapping(value = "noticeDetailWeb/{noticeId}" ,method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String findNoticeDetail(HttpServletRequest request,@PathVariable("noticeId")String noticeId){
		if(!NumberUtils.isDigits(noticeId)){
			//参数有误
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		String noticeContent = noticeService.findNoticeDeailWebById(NumberUtils.toInt(noticeId));
		
		return Response.success(noticeContent);
	}
	
	@RequestMapping(value = "appList" ,method = RequestMethod.POST ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String appNoticeList (HttpServletRequest request ) throws EmptyTokenException, IllegalTokenException{
		
		String pageNow = request.getParameter("pageNow");
		String pageSize = request.getParameter("pageSize");
		
		StoreAppAccount appAccount = this.getStoreAppAccount(request);
		
		int appAccountId = appAccount.getId();
		
		if(!NumberUtils.isDigits(pageNow)|| !NumberUtils.isDigits(pageSize) || appAccountId <= 0){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		List<MsgSend> sendList = noticeService.
				findAppNoticeListByPage(NumberUtils.toInt(pageNow),NumberUtils.toInt(pageSize),appAccountId);
		
		int pageCount = noticeService.getAppNoticePageCount(appAccountId);
		HashMap<String, Object> sendMap = new HashMap<>();
		sendMap.put("datas", sendList);
		sendMap.put("pageCount", pageCount);
		return Response.success(sendMap);
	} 
	
	/**
	 * 修改公告信息的查看状态 包括消息体的查看人数
	 */
	@RequestMapping(value = "changeNoticeStatus/{sendId}" ,method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String changeNoticeStatus(HttpServletRequest request ,@PathVariable("sendId") String sendId){
		if(!NumberUtils.isDigits(sendId)){
			//参数有误
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		noticeService.changeSendStatus(NumberUtils.toInt(sendId));
		return Response.success();
	} 
	
	/**
	 * 查询当前用户所有未读消息
	 * @throws IllegalTokenException 
	 * @throws EmptyTokenException 
	 */
	@RequestMapping(value = "uncheckedMsgList" ,method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String appUnchecedMsg(HttpServletRequest request ) throws EmptyTokenException, IllegalTokenException{
		
		StoreAppAccount account = this.getStoreAppAccount(request);
		
		int appAccountId = account.getId();
		
		if(appAccountId <= 0){
			//参数有误
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		List<MsgSend> uncheckedList = noticeService.findUncheckedMsg(appAccountId);
		HashMap<String, Object> sendMap = new HashMap<>();
		sendMap.put("datas", uncheckedList);
		return Response.success(sendMap);
	} 
	
	/**
	 * 后台发布公告
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "sendNoticeMsg" , method = RequestMethod.POST ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@Authentication
	public String sendNoticeMsg(HttpServletRequest request){
		String title = request.getParameter("title");
		String discribe = request.getParameter("discribe");
		String content = request.getParameter("content");
		//操作者id
		Account account = this.getCommonUserAccount(request);
		if(account == null){
			//登录状态失效，返回重新登录 
			return Response.error(MessageCode.ERROR_NO_LOGIN);
		}
		int operatorId = account.getId();
		int companyId = account.getCompanyId();
		if(operatorId <= 0 || companyId<=0){
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		
		MsgNotice msgNotice = new MsgNotice();
		msgNotice.setTitle(title);
		msgNotice.setDiscribe(discribe);
		msgNotice.setContent(content);
		msgNotice.setOperatorId(operatorId);
		msgNotice.setCompanyId(companyId);
		String json = request.getParameter("data");
		
		
		ArrayList<HashMap<String, Object>> data = new ArrayList<>();
		try {
			if(!StringUtils.isBlank(json)){
				data = (ArrayList<HashMap<String, Object>>) JSONParser.parse(json, data.getClass());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return Response.error(MessageCode.JSON_WRONG);
		}
		
		noticeService.sendNoticeMsg(msgNotice,data);
		return Response.success();	
	}
	
	/**
	 * 根据id查询公告详情
	 */
	@RequestMapping(value = "noticeDetail/{msgSendId}" ,method = RequestMethod.GET ,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String findDetailById(HttpServletRequest request , @PathVariable("msgSendId")String msgSendId){
		if(!NumberUtils.isDigits(msgSendId)){
			//参数异常
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		String content = noticeService.findNoticeDeailById(NumberUtils.toInt(msgSendId));
		
		return Response.success(content);
	}
	
	
	
	/**
	 * 根据id删除公告
	 */
	@RequestMapping(value = "removeNotice/{noticeId}")
	@ResponseBody
	@Authentication
	public String removeNotice(HttpServletRequest request ,@PathVariable("noticeId")String noticeId){
		if(!NumberUtils.isDigits(noticeId)){
			//参数异常
			return Response.error(MessageCode.PARAMETER_WRONG);
		}
		noticeService.removeNotice(NumberUtils.toInt(noticeId));
		return Response.success("删除成功");
	}
}





















